package com.edu.vodservicec.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.edu.commonutils.R;
import com.edu.vodservicec.service.VodService;
import com.edu.vodservicec.utils.InitDefaultAcsClientObject;
import com.edu.vodservicec.utils.VodProperties;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @ClassName VodServiceImpl
 * @date 2022/9/20 11:34
 * @Version 1.0
 */
@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadService(MultipartFile file)  {
        try {
        //1.音视频上传-本地文件上传

        //获取实际名称
        String fileName = file.getOriginalFilename();
            //文件后缀
            String title =  fileName.substring(0, fileName.lastIndexOf("."));;
            //获取流上传
        InputStream inputStream = file.getInputStream();

        //流式文件上传
            UploadStreamRequest request = new UploadStreamRequest(VodProperties.ACCESS_KEY_ID, VodProperties.ACCESS_KEY_SECRET, title, fileName,inputStream);

            //手动设置阿里云存储区域地址 //设置存储地址
        request.setStorageLocation("outin-9ac8aa66387e11edbd2100163e108a8f.oss-cn-beijing.aliyuncs.com");
        //手动设置服务接入点
        request.setApiRegionId("cn-beijing");

        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
            System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
        String videoid=null;
        if (response.isSuccess()) {
            videoid=response.getVideoId();
        } else {
            videoid=response.getVideoId();
        }

        return videoid;

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public R del(String id) {
        /*请求示例*/
        try {
        //初始化对象
        DefaultAcsClient client = null;
            client = InitDefaultAcsClientObject.initVodClient(VodProperties.ACCESS_KEY_ID, VodProperties.ACCESS_KEY_SECRET );
                //获取请求对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //设置值
            request.setVideoIds(id);

            //发送请求
            client.getAcsResponse(request);

                return  R.ok();
        } catch (ClientException e) {
//            try {
//                throw new MyException(Results.ERROR,"删除文件失败");
//            } catch (MyException ex) {
//                throw new RuntimeException(ex);
//            }
            e.printStackTrace();
        }
        return null;

    }

    @Override
    public void delAll(List vidList) {
        /*请求示例*/
        try {
            //初始化对象
            DefaultAcsClient client = null;
            client = InitDefaultAcsClientObject.initVodClient(VodProperties.ACCESS_KEY_ID, VodProperties.ACCESS_KEY_SECRET );
            //获取请求对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            String video = StringUtils.join(vidList,",");
            //设置值
            request.setVideoIds(video);

            //发送请求
            client.getAcsResponse(request);

        } catch (ClientException e) {
//            try {
//                throw new MyException(Results.ERROR,"删除文件失败");
//            } catch (MyException ex) {
//                throw new RuntimeException(ex);
//            }
            e.printStackTrace();
        }
    }


}
